﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace VAdvantage.Panels
{
    public partial class DatePanel : UserControl
    {
        public enum DatePanelMode
        {
            Today,
            Day,
            Month,
            Year,
        }

        private static readonly DependencyProperty DatePanelStyleProperty = DependencyProperty.Register("DatePanelStyle",
            typeof(DatePanelMode), typeof(DatePanel), new PropertyMetadata(DatePanelMode.Year, new PropertyChangedCallback(OnDateStylePropertyChanged)));

        public DatePanelMode DatePanelStyle
        {
            get { return (DatePanelMode)GetValue(DatePanelStyleProperty); }

            set { SetValue(DatePanelStyleProperty, value); }

        }

        public DatePanel()
        {
            InitializeComponent();
            Init();
        }

        private void Init()
        {

            cmbD.Items.Add(0);
            for (int i = 1; i <= 99; i++)
            {
                if (i < 32)
                {
                    cmbD.Items.Add(i);
                }

                cmbY.Items.Add(i);

                if (i < 13)
                {
                    cmbM.Items.Add(i);
                }
            }
            cmbD.SelectedIndex = 0;
            cmbM.SelectedIndex = 0;
            cmbY.SelectedIndex = 0;

        }


        private static void OnDateStylePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((DatePanel)d).OnDateStylePropertyChanged((DatePanelMode)e.OldValue, (DatePanelMode)e.NewValue);
        }

        private void OnDateStylePropertyChanged(DatePanelMode dateMode, DatePanelMode dateMode_2)
        {
            this.IsEnabled = true;
            Year.Visibility = System.Windows.Visibility.Visible;
            Month.Visibility = System.Windows.Visibility.Visible;
            Day.Visibility = System.Windows.Visibility.Visible;

            if (cmbM.Items.Count > 12)
            {
                cmbM.Items.RemoveAt(0);
            }

            if (dateMode_2 == DatePanelMode.Year)
            {
                cmbM.Items.Insert(0, 0);
            }

            else if (dateMode_2 == DatePanelMode.Month)
            {
                Year.Visibility = System.Windows.Visibility.Collapsed;
            }
            else if(dateMode_2 == DatePanelMode.Day)
            {
                Year.Visibility = System.Windows.Visibility.Collapsed;
                Month.Visibility = System.Windows.Visibility.Collapsed;
            }
            else if (dateMode_2 == DatePanelMode.Today)
            {
                Year.Visibility = System.Windows.Visibility.Collapsed;
                Month.Visibility = System.Windows.Visibility.Collapsed;
                this.IsEnabled = false;
            }

            cmbY.SelectedIndex = 0;
            cmbM.SelectedIndex = 0;
            cmbD.SelectedIndex = 0;
        }

        internal void SetDateStyle(int index)
        {
            if (index < 0)
            {
                return;
            }

            switch (index)
            {
                case 0:
                    DatePanelStyle = DatePanelMode.Today;
                    break;
                case 1:
                    DatePanelStyle = DatePanelMode.Day;
                    break;

                case 2:
                    DatePanelStyle = DatePanelMode.Month;
                    break;
                default:
                    DatePanelStyle = DatePanelMode.Year;
                    break;
            }
        }

        internal string GetText(int index)
        {
            string text = "";
            decimal timeUnit;
            if (DatePanelStyle == DatePanelMode.Year)
            {
                timeUnit = Decimal.Round(((decimal) GetTotalDays()/365),1);
                text = "Last " + timeUnit.ToString() + " Years";
            }
            else if (DatePanelStyle == DatePanelMode.Month)
            {
                timeUnit = Decimal.Round(((decimal)GetTotalDays() / 31), 1);
                text = "Last " + timeUnit.ToString() + " Month";
            }
            else
            {
                if (GetTotalDays() != 0)
                {
                    text = "Last " + GetTotalDays() + " Days";
                }
                else
                {
                    text = "This Day";
                }
            }
            return text;
        }

        public int GetTotalDays()
        {
            int totasldays;
            if (DatePanelStyle == DatePanelMode.Year)
            {
                totasldays = ((cmbY.SelectedIndex+1) * 365) + (cmbM.SelectedIndex * 31) + (cmbD.SelectedIndex);
            }
            else if (DatePanelStyle == DatePanelMode.Month)
            {
                totasldays = ((cmbM.SelectedIndex+1) * 31) + (cmbD.SelectedIndex);
            }
            else
            {
               totasldays  =cmbD.SelectedIndex;
            }

            return totasldays;
        }


        public string GetValue(int index)
        {
            string text = "";
            text = " adddays(sysdate, - " + GetTotalDays() + ") ";
            return text;
        }
    }
}
